<script setup lang="ts">
import { reactive, ref, onMounted } from "vue";
import { useMenu } from "./utils/hook";
import { transformI18n } from "@/plugins/i18n";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import organTree from "@/components/organTree.vue";
import productTree from "@/components/productTree.vue";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "powerList"
});
let treeInfoRef = ref(null);
let treeInfoRef2 = ref(null);
const formRef = ref();
const tableRef = ref();
let {
  changeRole,
  changesystem,
  saveHandle,
  selectPag,
  pagination,
  eidtformRef,
  editform,
  closeDialog,
  formVisible,
  roleOptions3,
  roleOptions4,
  roleOptions5,
  roleOptions,
  roleOptions2,
  onChange,
  loading3,
  columns,
  dataList,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handleSelectionChange,
  handleSizeChange,
  handleCurrentChange
} = useMenu();
//选择机构
const selectHandle = () => {
  treeInfoRef.value.getgetComanyTreeInfo();
  treeInfoRef.value.dialogTree = true;
};
//机构
const cropper = data => {
  editform.value.permitCompanyName = data.checName.join(",");
  editform.value.permitCompanyCode = data.checKay.join(",");
};
//选择产品
const selectHandle2 = () => {
  treeInfoRef2.value.dialogTree = true;
};
//产品
const cropper2 = data => {
  editform.value.permitProductName = data.checName;
  editform.value.permitProductCode = data.checKay;
};

function onFullscreen() {
  // 重置表格高度
  tableRef.value.setAdaptive();
}
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="机构：" prop="comCode">
        <el-select
          v-model="selectPag.comCode"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="(item, index) in roleOptions"
            :key="index"
            :value="item.comCode"
            :label="item.comCname"
          >
            {{ item.comCname }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="岗位：" prop="name">
        <el-select
          v-model="selectPag.name"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="(item, index) in roleOptions3"
            :key="index"
            :value="item.codeCode"
            :label="item.codeName"
          >
            {{ item.codeName }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="员工：" prop="userCode">
        <el-select
          v-model="selectPag.userCode"
          placeholder="请选择"
          clearable
          class="!w-[180px]"
        >
          <el-option
            v-for="(item, index) in roleOptions2"
            :key="index"
            :value="item.codeCode"
            :label="item.codeName"
          >
            {{ item.codeName }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading3"
          @click="onSearch"
        >
          搜索
        </el-button>
        <!-- <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button> -->
      </el-form-item>
    </el-form>

    <PureTableBar
      title="员工授权列表"
      :columns="columns"
      :isExpandAll="false"
      :tableRef="tableRef?.getTableRef()"
      @refresh="onSearch"
      @fullscreen="onFullscreen"
    >
      <el-table
        :data="dataList"
        style="width: 100%"
        stripe
        border
        :header-cell-style="{
          background: 'var(--el-fill-color-light)',
          color: 'var(--el-text-color-primary)'
        }"
      >
        <el-table-column type="expand">
          <template #default="props">
            <div m="4">
              <el-table
                :data="props.row.roleInfo"
                border
                stripe
                :header-cell-style="{
                  background: 'var(--el-fill-color-light)',
                  color: 'var(--el-text-color-primary)'
                }"
              >
                <el-table-column label="角色名称" prop="roleName" />
                <el-table-column label="应用系统" prop="systemName" />
                <el-table-column label="授权时间" prop="authorizeTime" />
                <el-table-column label="状态（是否有效）" prop="valid">
                  <template #default="{ row }">
                    <div>
                      <span v-if="row.valid == '1'">有效</span>
                      <span v-if="row.valid == '0'">无效</span>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="员工姓名" prop="username" />
        <el-table-column label="归属机构" prop="comName" />
        <el-table-column label="岗位" prop="gradeName" />
        <el-table-column label="客户标识" prop="customerFlag" />
        <el-table-column label="入职时间" prop="entryTime" />
        <el-table-column label="状态" prop="enabled">
          <template #default="{ row }">
            <div>
              <span v-if="row.enabled == '1'">申请中</span>
              <span v-if="row.enabled == '2'">已入职</span>
              <span v-if="row.enabled == '3'">离职</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" prop="name">
          <template #default="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog(row)"
            >
              授权
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 20px 0; display: flex; justify-content: end">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[100, 200, 300, 400]"
          size="default"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="selectPag.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </PureTableBar>
    <el-dialog
      v-model="formVisible"
      title="授权"
      width="65%"
      draggable
      :before-close="closeDialog"
    >
      <el-form
        ref="eidtformRef"
        :inline="true"
        :model="editform"
        label-width="120px"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
      >
        <el-form-item label="员工姓名：" prop="username">
          <el-input
            v-model="editform.username"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工账号：" prop="userCode">
          <el-input
            v-model="editform.userCode"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="归属机构：" prop="comName">
          <el-input
            v-model="editform.comName"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="岗位：" prop="gradeName">
          <el-input
            v-model="editform.gradeName"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工状态：" prop="enabled">
          <el-input
            v-model="editform.enabled"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="客户标识：" prop="customerFlag">
          <el-input
            v-model="editform.customerFlag"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <el-form-item label="入职时间：" prop="entryTime">
          <el-input
            v-model="editform.entryTime"
            disabled
            class="!w-[360px]"
          ></el-input>
        </el-form-item>
        <br />
        <el-form-item label="允许机构：" prop="permitCompanyName">
          <el-input
            v-model="editform.permitCompanyName"
            placeholder="请选择允许机构"
            clearable
            disabled
            class="!w-[300px]"
          />
          <el-button type="primary" @click="selectHandle">选择</el-button>
        </el-form-item>
        <el-form-item label="是否除外：" prop="companyFlag">
          <el-select
            v-model="editform.companyFlag"
            placeholder="请选择"
            clearable
            class="!w-[360px]"
          >
            <el-option label="是" value="1" />
            <el-option label="否" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="允许产品：" prop="permitProductName">
          <el-input
            v-model="editform.permitProductName"
            placeholder="请选择允许产品"
            clearable
            disabled
            class="!w-[300px]"
          />
          <el-button type="primary" @click="selectHandle2">选择</el-button>
        </el-form-item>
        <el-form-item label="是否除外：" prop="productFlag">
          <el-select
            v-model="editform.productFlag"
            placeholder="请选择"
            clearable
            class="!w-[360px]"
          >
            <el-option label="是" value="1" />
            <el-option label="否" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="应用系统：" prop="systemCode">
          <el-select
            v-model="editform.systemCode"
            placeholder="请选择"
            clearable
            class="!w-[360px]"
            @change="changesystem"
          >
            <el-option
              v-for="(item, index) in roleOptions4"
              :key="index"
              :value="item.applicationCode"
              :label="item.applicationName"
            >
              {{ item.applicationName }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色：" prop="roleId">
          <el-select
            v-model="editform.roleId"
            placeholder="请选择"
            clearable
            class="!w-[360px]"
            @change="changeRole"
          >
            <el-option
              v-for="(item, index) in roleOptions5"
              :key="index"
              :value="item.roleId"
              :label="item.name"
            >
              {{ item.name }}
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <h3 style="padding: 20px 0 20px">角色列表</h3>
      <el-table
        :data="editform.roleInfo"
        stripe
        border
        :header-cell-style="{
          background: 'var(--el-fill-color-light)',
          color: 'var(--el-text-color-primary)'
        }"
      >
        <el-table-column label="角色名称" prop="roleName" />
        <el-table-column label="应用系统" prop="systemName" />
        <el-table-column label="授权时间" prop="authorizeTime" />
        <el-table-column label="状态（是否有效）" prop="valid">
          <template #default="{ row }">
            <div>
              <span v-if="row.valid == '1'">有效</span>
              <span v-if="row.valid == '0'">无效</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <el-button type="primary" @click="saveHandle"> 提交 </el-button>
        <el-button @click="closeDialog"> 取消 </el-button>
      </template>
    </el-dialog>
    <organTree ref="treeInfoRef" @cropper="cropper"></organTree>
    <productTree ref="treeInfoRef2" @cropper="cropper2"></productTree>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-table__inner-wrapper::before) {
  height: 0;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
